/**
 * Apr 1, 2010
 */
package org.tomale.id.documents.management.ui.preferences.pages;

import java.util.List;

import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
import org.tomale.id.common.KeyValuePair;
import org.tomale.id.documents.management.Activator;
import org.tomale.id.documents.management.DocumentManager;
import org.tomale.id.documents.management.PreferenceConstants;

/**
 * @author ferd
 *
 */
public class DocumentManagementPage extends PreferencePage implements
		IWorkbenchPreferencePage {

	Combo _stores;
	
	List<KeyValuePair<String, String>> _providers;
	
	public DocumentManagementPage(){
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	public DocumentManagementPage(String title){
		super(title);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	public DocumentManagementPage(String title, ImageDescriptor image){
		super(title,image);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		l = new Label(comp, SWT.NONE);
		l.setText("Document Store");
		l.setLayoutData(new GridData());
	
		_stores = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		_stores.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));
		initStores();
		
		setPreferences();
		
		return comp;
	}
	
	private boolean valid(){
		return true;
	}

	@Override
	public boolean performOk() {
		if(valid()){
			try {
				save();
				return super.performOk();
			}catch(BackingStoreException e){
				setErrorMessage(e.getMessage());
				return false;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub

	}
	
	private void initStores(){
		_providers = DocumentManager.getStores();
		for(KeyValuePair<String, String> kv : _providers){
			_stores.add(kv.getValue());
		}
	}
	
	private void setPreferences(){
		IPreferenceStore store = getPreferenceStore();
		if(!store.getString(PreferenceConstants.DOCUMENT_STORE_ID).isEmpty()){
			_stores.setText(getPairByKey(_providers, store.getString(PreferenceConstants.DOCUMENT_STORE_ID)).getValue());
		}
	}
	
	private void save() throws BackingStoreException{
		IPreferenceStore store = getPreferenceStore();
		store.setValue(PreferenceConstants.DOCUMENT_STORE_ID, 
				getPairByValue(_providers, _stores.getText()).getKey());
		
		IEclipsePreferences pref = new ConfigurationScope().getNode(Activator.PLUGIN_ID);
		pref.flush();
	}
	
	private KeyValuePair<String, String> getPairByKey(final List<KeyValuePair<String, String>> pairs,
		final String key){
		for(KeyValuePair<String, String> kv : pairs){
			if(key.equals(kv.getKey())){
				return kv;
			}
		}
		return null;
	}

	private KeyValuePair<String, String> getPairByValue(final List<KeyValuePair<String, String>> pairs,
			final String value){
			for(KeyValuePair<String, String> kv : pairs){
				if(value.equals(kv.getValue())){
					return kv;
				}
			}
			return null;
		}	
}
